package com.zret.face.controllerDemo;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.zret.face.entity.check.CheckFaceRootBean;
import com.zret.face.entity.select.SelectFaceJsonRootBean;
import com.zret.facetools.FaceBDTools;
import com.zret.facetools.GsonUtils;

/**
 * Servlet implementation class FaceServlet
 */
@WebServlet("/FaceServlet")
public class FaceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FaceServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		//获取参数
		String imgbase64 = request.getParameter("imagebast64");
		//需要去掉base64的头，得到字符串数组
		String[] imgs= imgbase64.split("data:image/png;base64,");
		//拼接字符串
		String imgString="";
		for (int i = 0; i < imgs.length; i++) {
			imgString=imgString+imgs[i];
		}
		
		
        System.out.println("imgbase64"+imgString);
//		//调用工具类注册人脸
//     	JSONObject jsob = FaceBDTools.faceRegister(imgString, "1");
//     	System.out.println(jsob.toString());
        //人脸检测
        JSONObject faceChecking = FaceBDTools.faceChecking(imgString);
        String facechString = faceChecking.toString();
        //写入实体类
        CheckFaceRootBean fromJson = GsonUtils.fromJson(facechString, CheckFaceRootBean.class);
        int error_code = fromJson.getError_code();
        String error_msg = fromJson.getError_msg();
        System.out.println("人脸检测错误码:"+error_code+"错误信息："+error_msg);
        //人脸搜索
        JSONObject faceSelect = FaceBDTools.faceSelect(imgString);
        String faceSelectString = faceSelect.toString();
        SelectFaceJsonRootBean fromJson2 = GsonUtils.fromJson(faceSelectString, SelectFaceJsonRootBean.class);
	    String error_msg2 = fromJson2.getError_msg();
	    double score = fromJson2.getResult().getUser_list().get(0).getScore();
	    String user_id = fromJson2.getResult().getUser_list().get(0).getUser_id();
        System.out.println("人脸搜索分数"+error_msg2+"人脸搜索到用户id"+user_id+"人脸搜索的比对分数"+score);
        
        
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
